package com.edu.vodservicec.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.edu.commonutils.R;
import com.edu.commonutils.Results;
import com.edu.servicebase.exceptionhandler.MyException;
import com.edu.vodservicec.service.VodService;
import com.edu.vodservicec.utils.InitDefaultAcsClientObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName VodController
 * @date 2022/9/20 11:32
 * @Version 1.0
 */
@Api(description = "文件上传接口")
//@CrossOrigin
@RestController
@RequestMapping("/vodservice/video")
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频给阿里云视频点播
    @PostMapping("/uploadVideo")
    @ResponseBody
    public R uploadAliyun( MultipartFile file){
        //返回视频上传id
        String id= null;
        try {
            id = vodService.uploadService(file);
        } catch (IOException e) {
            try {
                throw new MyException(Results.ERROR,"文件上传失败");
            } catch (MyException ex) {
                throw new RuntimeException(ex);
            }
        }

        return R.ok().data("videoid",id);
    }

    @DeleteMapping("/delVideo/{id}")
    @ResponseBody
    public R del(@PathVariable String id){
        //删除文件
        R r = vodService.del(id);
        return r;
    }
    //删除多个视频
    @DeleteMapping("/delVideoAll")
    @ResponseBody
    public R delmany(@RequestParam("vidList") List<String> vidList){
        //删除文件
       vodService.delAll(vidList);
        return R.ok();
    }

        //拿到视频的播放地址
        @PostMapping("/edit/{id}")
        @ResponseBody
        public String getVideoPath(@PathVariable String id)  {
        try {

            //根据视屏id获取视屏播放地址
            //初始化对象
            DefaultAcsClient client = InitDefaultAcsClientObject.initVodClient("LTAI5t9KuQZUZGA6PUp7Zpz7", "VJKkVvBPxlqZduLZu0ccbcZ9HPLXq4");
            //获取创建视屏地址的request和response
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            GetPlayInfoResponse response = new GetPlayInfoResponse();
            //向request对象设置视频id
            request.setVideoId(id);

            //调用初始化对象里面的方法,传递request,获取数据
            response = client.getAcsResponse(request);

            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            String url="";
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                url =   playInfo.getPlayURL();
            }
            return url;
        }catch (Exception e){
            try {
                throw new MyException(Results.ERROR,"返回上传地址错误");
            } catch (MyException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    @ApiOperation("获取视频播放凭证")
    @PostMapping("/getauth/{id}")
    @ResponseBody
    public R getAuth(@PathVariable String id) throws Exception {

            //初始化客户端、请求对象和相应对象
            DefaultAcsClient client = InitDefaultAcsClientObject.initVodClient("LTAI5t9KuQZUZGA6PUp7Zpz7", "VJKkVvBPxlqZduLZu0ccbcZ9HPLXq4");
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            //设置请求参数
            request.setVideoId(id);
            //获取请求响应
            response = client.getAcsResponse(request);
            System.out.println("进来******"+response);
            //获取凭证
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
    }

}
